package com.ermax.aircond.util.listener;

import java.io.Serializable;
import org.hibernate.envers.EntityTrackingRevisionListener;
import org.hibernate.envers.RevisionType;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.ermax.aircond.common.domain.GateData;
import com.ermax.aircond.common.domain.ReadMachineData;
import com.ermax.aircond.common.domain.UserDetail;
import com.ermax.aircond.common.domain.UserGroup;
import com.ermax.aircond.common.domain.WriteMachineData;
import com.ermax.aircond.maintain.domain.Building;
import com.ermax.aircond.maintain.domain.Floor;
import com.ermax.aircond.maintain.domain.Machine;
import com.ermax.aircond.maintain.domain.Room;
import com.ermax.aircond.manage.domain.ScheduleTerm;

//@Service("auditedListener")
public class AuditedListenerBAK implements EntityTrackingRevisionListener, ApplicationContextAware {

	/** spring context */
	private static ApplicationContext applicationContext;

	public void newRevision(Object revisionEntity) {

	}

	@Override
	public void entityChanged(@SuppressWarnings("rawtypes") Class entityClass, String entityName, Serializable entityId, RevisionType revisionType, Object revisionEntity) {
		/*
		AuditedRevEntity revEntity = (AuditedRevEntity) revisionEntity;

		String username = "ANONYMOUS";
		if (SecurityContextHolder.getContext() != null 
				&& SecurityContextHolder.getContext().getAuthentication() != null				
				&& SecurityContextHolder.getContext().getAuthentication().getPrincipal() != null) {
			
			UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			username = userDetails.getUsername();
		}
		revEntity.setUsername(username);

		String classType = entityClass.getSimpleName();
		revEntity.setClassType(classType);

		long id = 0L;
		if (entityId instanceof Long) {
			id = (Long) entityId;
			revEntity.setEntityId(id);
		}else if(entityId instanceof Integer){
			id = (Integer) entityId;
			revEntity.setEntityId(id);
		}else if(entityId instanceof Short){
			id = (Short) entityId;
			revEntity.setEntityId(id);
		}

		StringBuilder sbDesc = new StringBuilder();

		sbDesc.append("(");
		if (revisionType == RevisionType.ADD) {
			sbDesc.append("ADD");
		} else if (revisionType == RevisionType.MOD) {
			sbDesc.append("MOD");
		} else if (revisionType == RevisionType.DEL) {
			sbDesc.append("DEL");
		} else {

		}
		sbDesc.append(") ");

		
		AuditLogService auditLogService = (AuditLogService) getApplicationContext().getBean("auditLogService");
		
		int versionId = revEntity.getId();
		if (revisionType == RevisionType.DEL) {
			
			List<Number> numbers = null;
			
			if (entityId instanceof Long) {
				numbers = auditLogService.getRevNumbers(entityClass, (Long)entityId);
			}else if(entityId instanceof Integer){
				numbers = auditLogService.getRevNumbers(entityClass, (Integer)entityId);
			}else if(entityId instanceof Short){
				numbers = auditLogService.getRevNumbers(entityClass, (Short)entityId);
			}
						
			if(numbers.size() >= 2){
				versionId = (Integer) numbers.get(numbers.size() -2);
			}
		}

		try {	
			Object logObject = null;
			if (entityId instanceof Long) {
				logObject = auditLogService.getDomainByVersion(entityClass, (Long)entityId, versionId);
			}else if(entityId instanceof Integer){
				logObject = auditLogService.getDomainByVersion(entityClass, (Integer)entityId, versionId);
			}else if(entityId instanceof Short){
				logObject = auditLogService.getDomainByVersion(entityClass, (Short)entityId, versionId);
			}
			
			sbDesc.append(getDescription(logObject));
		} catch (Exception e) {
			sbDesc.append("UNKNOWN");
		}
		
		revEntity.setDescription(sbDesc.toString());
		*/
	}

	@SuppressWarnings("unused")
	private String getDescription(Object logObject ) {

		StringBuilder sb = new StringBuilder();
		try{
			if(logObject instanceof UserGroup){
				UserGroup userGroup = (UserGroup) logObject;
				sb.append(userGroup.getUserGroupId());
				sb.append(" - ");
				sb.append(userGroup.getGroupName());
				
			}else if(logObject instanceof UserDetail){
				UserDetail userDetail = (UserDetail) logObject;
				sb.append(userDetail.getUserId());
				sb.append(" - ");
				sb.append(userDetail.getUsername());
				/*
				sb.append("[");
				List<UserGroup> userGroups = userDetail.getUserGroups();
				Iterator<UserGroup> iterator = userGroups.iterator();
				while(iterator.hasNext()){
					UserGroup userGroup = iterator.next();					
					sb.append(userGroup.getGroupName());
					sb.append("/");
				}
				sb.append("]");
				*/
			}else if(logObject instanceof Building){
				Building building = (Building) logObject;
				sb.append(building.getBuildingId());
				sb.append(" - ");
				sb.append(building.getBuildingName());
				
			}else if(logObject instanceof Floor){
				Floor floor = (Floor) logObject;
				sb.append(floor.getFloorId());
				sb.append(" - ");
				sb.append(floor.getFloorName());
				/*
				Building building = floor.getBuilding();
				if(building != null){
					sb.append(", ");					
					sb.append(building.getBuildingName());
				}
				*/				
			}else if(logObject instanceof Room){
				Room room = (Room) logObject;
				sb.append(room.getRoomId());
				sb.append(" - ");
				sb.append(room.getRoomName());
				/*
				Floor floor = room.getFloor();
				if(floor != null){					
					sb.append(", ");
					sb.append(floor.getFloorName());
				}
								
				Building building = floor.getBuilding();
				if(building != null){
					sb.append(", ");					
					sb.append(building.getBuildingName());
				}
				*/				
			}else if(logObject instanceof Machine){
				
				Machine machine = (Machine) logObject;				
				sb.append(machine.getMachineId());
				sb.append(" - ");
				sb.append(machine.getMachineName());
				/*
				Room room = machine.getRoom();
				sb.append(room.getRoomId());
				sb.append(" - ");
				sb.append(room.getRoomName());
				
				Floor floor = room.getFloor();
				if(floor != null){					
					sb.append(", ");
					sb.append(floor.getFloorName());
				}
								
				Building building = floor.getBuilding();
				if(building != null){
					sb.append(", ");					
					sb.append(building.getBuildingName());
				}
				*/				
			}else if(logObject instanceof GateData){
				GateData gateData = (GateData) logObject;
											
				sb.append(gateData.getGid());
				sb.append(" - ");
				sb.append(gateData.getName());
				sb.append(", ");
				sb.append(gateData.getIp());
				
			}else if(logObject instanceof ReadMachineData){
				ReadMachineData data = (ReadMachineData) logObject;										
				sb.append(data.getNid());
				sb.append(" - ");
				sb.append(data.getName());
				sb.append(", ");
				sb.append(data.getRcmd());
				
			}else if(logObject instanceof WriteMachineData){
				WriteMachineData data = (WriteMachineData) logObject;										
				sb.append(data.getNid());
				sb.append(" - ");
				sb.append(data.getCmd());
					
			}
			
			else if(logObject instanceof ScheduleTerm){
				ScheduleTerm scheduleTerm = (ScheduleTerm) logObject;
				sb.append(scheduleTerm.getNid());
				sb.append(" - ");
				sb.append(scheduleTerm.getCmd());
				sb.append(", ");
				if(scheduleTerm.isActive()){
					sb.append("Y, ");
				}else{
					sb.append("N, ");
				}
				sb.append(scheduleTerm.getScheduleHour());
				sb.append(":");
				sb.append(scheduleTerm.getScheduleMin());
				sb.append("(");
				if(scheduleTerm.isExecMon()){
					sb.append("1/");
				}
				if(scheduleTerm.isExecTue()){
					sb.append("2/");
				}
				if(scheduleTerm.isExecWed()){
					sb.append("3/");
				}
				if(scheduleTerm.isExecThu()){
					sb.append("4/");
				}
				if(scheduleTerm.isExecFri()){
					sb.append("5/");
				}
				if(scheduleTerm.isExecSat()){
					sb.append("6/");
				}
				if(scheduleTerm.isExecSun()){
					sb.append("7/");
				}
				
				sb.append(")");
			}
			
			
			
		}catch (Exception e) {
			sb.append(logObject.getClass().getName());
		}
		
		return sb.toString();

	}

	/**
	 * @param pApplicationContext
	 * @throws BeansException
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
	 */
	public void setApplicationContext(ApplicationContext pApplicationContext) throws BeansException {
		applicationContext = pApplicationContext;
	}

	/**
	 * @return spring applicationContext
	 */
	@SuppressWarnings("unused")
	private static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

}
